package xuegao.study.mall.rbac.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.common.api.CommonPage;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.domain.UserDto;
import xuegao.study.mall.common.dto.rbac.UmsAdminRegisterDto;
import xuegao.study.mall.common.model.ums.UmsAdmin;
import xuegao.study.mall.common.model.ums.UmsRole;
import xuegao.study.mall.rbac.service.UmsAdminService;
import java.util.List;

@Api(tags = "UmsAdminController")
@Tag(name = "UmsAdminController", description = "后台用户管理相关接口")
@RestController
@RequestMapping(value = "/admin")
public class UmsAdminController {

    @Autowired
    private UmsAdminService umsAdminService;

    @ApiOperation("注册用户")
    @PostMapping("/register")
    public CommonResult<Integer> register(@RequestBody UmsAdminRegisterDto registerDto) {
        int count = umsAdminService.register(registerDto);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除用户")
    @PostMapping("/delete/{id}")
    public CommonResult<Integer> deleteAdmin(@PathVariable("id") Long id) {
        int count = umsAdminService.deleteById(id);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("更新用户状态")
    @PostMapping(value = "/updateStatus/{adminId}")
    public CommonResult updateAdminStatus(@PathVariable("adminId") Long adminId, @RequestParam("status") Integer status) {
        int count = umsAdminService.updateAdminStatus(adminId, status);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("更新用户信息")
    @PostMapping("/update/{id}")
    public CommonResult<Integer> update(@PathVariable("id") Long id, @RequestBody UmsAdmin umsAdmin) {
        int count = umsAdminService.updateAdmin(id, umsAdmin);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "获取当前登录用户信息")
    @GetMapping("/info")
    public CommonResult getAdminInfo(@RequestParam String userStr) {
        return umsAdminService.getAdminInfo(userStr);
    }

    @ApiOperation("获取用户列表")
    @GetMapping("/list")
    public CommonResult<CommonPage<UmsAdmin>> listAdmin(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "name", defaultValue = "") String name){
        List<UmsAdmin> list = umsAdminService.selectList(pageNum, pageSize, name);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("获取用户-角色列表")
    @GetMapping("/role/{adminId}")
    public CommonResult<List<UmsRole>> adminRoles(@PathVariable("adminId") Long adminId) {
        List<UmsRole> list = umsAdminService.adminRoles(adminId);
        return CommonResult.success(list);
    }

    @ApiOperation("更新用户-分配角色")
    @PostMapping("/role/update")
    public CommonResult<Integer> allocRoles(@RequestParam Long adminId, @RequestParam List<Long> roleIds) {
        int count = umsAdminService.allocRoles(adminId, roleIds);
            if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("根据用户名获取通用用户信息")
    @GetMapping(value = "/loadByUsername")
    public UserDto loadUserByUsername(@RequestParam String username) {
        UserDto userDto = umsAdminService.getUserByUsername(username);
        return userDto;
    }

}
